/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-12-02 19:40:58
 * Copyright (c) 2015- joysuch-版权所有
 */

package com.joysuch.wwyt.bp.repository;

import com.joysuch.wwyt.bp.entity.BpEnterpriseInfo;
import com.joysuch.wwyt.datasync.annotation.DataSync;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * BpEnterpriseInfoDao
 */
public interface BpEnterpriseInfoDao extends JpaRepository<BpEnterpriseInfo, Long> {
    Page<BpEnterpriseInfo> findAll(Specification<BpEnterpriseInfo> spec, Pageable pageable);

    @Override
    BpEnterpriseInfo save(BpEnterpriseInfo bean);

    @Override
    void delete(BpEnterpriseInfo bean);

    BpEnterpriseInfo findAllByOrgCodeAndTenentId(String Code,Long tenantId);

    @DataSync(selectSql = "select * from wwyt_bp_enterprise_info where id=?1", argsIndex = { 2 }, nativeQuery = true)
    @Transactional
    @Modifying
    @Query(value = "update wwyt_bp_enterprise_info set NAME=?1 ,SURROUNDING_PEOPLE_COUNT=?2 where ID =?3", nativeQuery = true)
    void updateName(String name, Integer pCount, Long id);

    @DataSync(selectSql = "select * from wwyt_bp_enterprise_info where id=?1", argsIndex = { 2 }, nativeQuery = true)
    @Transactional
    @Modifying
    @Query(value = "update wwyt_bp_enterprise_info set name=?1 where tenent_id =?2 and org_code = ?3", nativeQuery = true)
    void updateNameByTenentIdAndOrgCode(String name, Long tenentId, String orgCode);

    List<BpEnterpriseInfo> findAllByTenentId(Long tenentId);

    @Query(value = "select * from wwyt_bp_enterprise_info where NAME = ?1 and TENENT_ID = ?2 and ORG_CODE = ?3", nativeQuery = true)
    BpEnterpriseInfo findByCompanyName(String workCompany, Long tenentId, String orgCode);

    @Query(value = "select * from wwyt_bp_enterprise_info where NAME = ?1 and TENENT_ID = ?2 ", nativeQuery = true)
    BpEnterpriseInfo findByCompanyNameAndTenentId(String workCompany, Long tenentId);

    BpEnterpriseInfo findByOrgCode(String orgCode);

    @Query(value = "SELECT DISTINCT TENENT_ID AS tenentId,ORG_CODE AS orgCode FROM wwyt_bp_enterprise_info", nativeQuery = true)
    List<Map<String, Object>> selectTenentIdAndOrgCodes();
}
